package com.xt.service.impl;

import com.xt.bean.Section;
import com.xt.dao.SectionRepository;
import com.xt.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by XT on 2018/5/4.
 */
@Service
public class SectionServiceImpl implements SectionService {

    @Autowired
    private SectionRepository sr;

    @Override
    public void saveOrUpdateOne(Section s){

        if (StringUtils.isEmpty(s.getId())){
            //新增
            if (!StringUtils.isEmpty(s.getFatherId())){
                //设置父亲有子节点
                Section FatherSec = sr.findOne(s.getFatherId());
                FatherSec.setHasChild(true);
                sr.save(FatherSec);
            }
            s.setCreateDate(new Date());
        }

        s.setUpdateDate(new Date());
        sr.save(s);
    }

    @Override
    public void deleteOne(String id){
        //删除关联关系

        sr.delete(id);
    }

    @Override
    public Section getById(String id){
        return sr.findOne(id);
    }

    @Override
    public  List<Section> getByCourseIdAndTeacherIdAndFatherIdIsNull(String courseId, String teacherId){
        return sr.findByCourseIdAndTeacherId(courseId,teacherId);
    }

    @Override
    public List<Section> getChilds(String fatherId) {
        List<Section> sectionList = sr.findByFatherId(fatherId);
        return sectionList;
    }



}

